package com.cangchu.widget;


import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import com.cangchu.R;
import com.cangchu.util.BitmapUtils;

public class HomeButton extends ImageView {

    private Bitmap bitmap;
    private Bitmap home_flight;
    private int state = 0; // 按下

    private int color;
    private float textsize;
    private boolean big;
    private int home;
    private String text;

    private int screenW;
    private int screenH;

    // 点击事件
    private HomeClickListener listener = null;

    public interface HomeClickListener {


        public void onclick();
    }

    @Override
    public boolean isInEditMode() {
        return super.isInEditMode();
    }

    private int[] colors = {getResources().getColor(R.color.main_red),
            getResources().getColor(R.color.main_orange),
            getResources().getColor(R.color.main_blue),
            getResources().getColor(R.color.main_purple),
            getResources().getColor(R.color.main_air),
            getResources().getColor(R.color.main_texi),
            getResources().getColor(R.color.main_jingdian),
            getResources().getColor(R.color.main_signin),
            getResources().getColor(R.color.main_signout)};

    private Bitmap[] bitmaps = {
            BitmapFactory.decodeResource(getResources(), R.drawable.main_routeset),
            BitmapFactory.decodeResource(getResources(),
                    R.drawable.main_purchase),
            BitmapFactory.decodeResource(getResources(),
                    R.drawable.main_inspection),
            BitmapFactory.decodeResource(getResources(),
                    R.drawable.main_inventory),
            BitmapFactory.decodeResource(getResources(),
                    R.drawable.main_returngoods),
            BitmapFactory.decodeResource(getResources(),
                    R.drawable.main_xunjian),
            BitmapFactory.decodeResource(getResources(),
                    R.drawable.main_broken),
            BitmapFactory.decodeResource(getResources(),
                    R.drawable.main_commset),
            BitmapFactory.decodeResource(getResources(),
                    R.drawable.main_record)

    };

    public HomeButton(Context context) {
        super(context);
    }

    public HomeButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode()) {
            bitmap = BitmapUtils.zoomImage(BitmapFactory.decodeResource(
                    getResources(), R.drawable.fingerprint), 127, 122);

            TypedArray typedArray = context.obtainStyledAttributes(attrs,
                    R.styleable.HomeButton);
            color = typedArray.getInt(R.styleable.HomeButton_backcolor, 0);
            textsize = typedArray.getDimension(R.styleable.HomeButton_textSize, 24);
            big = typedArray.getBoolean(R.styleable.HomeButton_big, true);
            home = typedArray.getInt(R.styleable.HomeButton_home, 0);
            text = typedArray.getString(R.styleable.HomeButton_text);
            System.out.println("color:" + color + " textsize:" + textsize + " big:"
                    + big + " home:" + home);
            home_flight = bitmaps[home];
            // 获取屏幕的大小，坑爹找了老半天

            screenW = ((Activity) context).getWindow().getWindowManager()
                    .getDefaultDisplay().getWidth() / 2 - 16;
            if (big) {
                screenH = screenW - 15;
            } else {
                screenH = (screenW - 15) / 2 - 4;
            }
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!isInEditMode()) {
            // 重新设置屏幕大小
            setMeasuredDimension(screenW, screenH);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

	/*
     * orange 2182F7 light red 7359EF 紫 B551A5 Blue CE8A39 air CEBE00 texi
	 * 9CAA00 jingdian 00AA73
	 */

    // 这里计算大小不要用cnavas的大小来计算，会出错，因为真机以模拟器中得到的大小不一样，具体为什么是这样还没有找到原因
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isInEditMode()) {
            canvas.drawColor(colors[color]);
            Paint paint = new Paint();
            paint.setColor(Color.WHITE);

            if (big) {
                paint.setTextSize(24);
                Matrix matrix = new Matrix();
                matrix.postTranslate(this.getWidth() / 2 - home_flight.getWidth()
                        / 2, this.getHeight() / 2 - home_flight.getHeight() / 2);
                canvas.drawText(text, 10, 40, paint);
                canvas.drawBitmap(home_flight, matrix, paint);
            } else {
                paint.setTextSize(22);
                Matrix matrix_small = new Matrix();
                matrix_small.postTranslate(10,
                        this.getHeight() / 2 - home_flight.getHeight() / 2);
                canvas.drawBitmap(home_flight, matrix_small, new Paint());
                canvas.drawText(text, home_flight.getWidth() - 10, this.getHeight()
                        / 2 + home_flight.getHeight() / 2, paint);
            }
            if (state == 1) {
                Matrix matrix2 = new Matrix();
                matrix2.postTranslate(this.getWidth() / 2 - bitmap.getWidth() / 2,
                        this.getHeight() / 2 - bitmap.getHeight() / 2);
                canvas.drawBitmap(bitmap, matrix2, new Paint());
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isInEditMode()) {
            float start = 1.0f;
            float end = 0.95f;
            Animation scaleAnimation = new ScaleAnimation(start, end, start, end,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                    0.5f);
            Animation endAnimation = new ScaleAnimation(end, start, end, start,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                    0.5f);
            scaleAnimation.setDuration(200);
            scaleAnimation.setFillAfter(true);
            endAnimation.setDuration(200);
            endAnimation.setFillAfter(true);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    this.startAnimation(scaleAnimation);
                    state = 1;
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    this.startAnimation(endAnimation);
                    state = 0;
                    invalidate();
                    if (listener != null) {
                        listener.onclick();
                    }
                    break;
                // 滑动出去不会调用action_up,调用action_cancel
                case MotionEvent.ACTION_CANCEL:
                    this.startAnimation(endAnimation);
                    state = 0;
                    invalidate();
                    break;
            }
            // 不返回true，Action_up就响应不了
            return true;
        }
        return true;
    }

    /**
     * 加入响应事件
     *
     * @param clickListener
     */
    public void setOnHomeClick(HomeClickListener clickListener) {
        this.listener = clickListener;
    }

}
